
{include file="layout/css"}
<!-- 引入UEditor相关文件 -->
<script type="text/javascript" src="/static/Ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/static/Ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="/static/Ueditor/lang/zh-cn/zh-cn.js"></script>
{include file="layout/header"}
<div class="fly-panel fly-column">
    <div class="layui-container">
        <ul class="layui-clear">
            <li class="layui-hide-xs"><a href="{:url('forum',['type'=>'all'])}">首页</a></li>
            <li class=""><a href="{:url('forum',['type'=>'bug'])}">提交BUG/建议</a></li>
            <li class=""><a href="{:url('forum',['type'=>'update'])}">【更新公告】</a></li>
        </ul>
    </div>
</div>

<div class="layui-container">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="fly-panel" style="margin-bottom: 0;">
                <div class="fly-panel-title fly-filter">
                    <a>发表新帖</a>
                </div>
                <div class="layui-form layui-form-pane">
                    <form method="post" id="forumForm" lay-filter="forumForm">
                        <div class="layui-form-item">
                            <label for="title" class="layui-form-label">标题</label>
                            <div class="layui-input-block">
                                <input type="text" id="title" name="title" required lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入标题">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-block" style="z-index: 1000">
                                <select name="type" lay-verify="required" style="z-index: 100">
                                    <option value="">请选择类型</option>
                                    <option value="bug">BUG反馈/建议</option>
                                    <option value="update">更新内容</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label for="content" class="layui-form-label">内容</label>
                            <div class="layui-input-block">
                                <!-- 将textarea修改为script标签，用于UEditor初始化 -->
                                <script id="editor" name="content" type="text/plain" style="width:100%;height:300px;"></script>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" lay-submit lay-filter="forumSubmit" type="submit">立即发布</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 初始化UEditor富文本编辑器
    var ue = UE.getEditor('editor', {
        toolbars: [[
            'fullscreen', 'source', '|', 'undo', 'redo', '|',
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
            'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
            'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
            'directionalityltr', 'directionalityrtl', 'indent', '|',
            'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
            'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
            'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
            'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
            'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
            'print', 'preview', 'searchreplace', 'drafts', 'help'
        ]],
        autoHeightEnabled: true,
        autoFloatEnabled: true,
        initialFrameHeight: 300
    });

    layui.use( function(){
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.$;
        
        // 检查用户是否登录
        var user = localStorage.getItem("user");
        if(!user){
            // layer.msg('请先登录后再发布', {icon: 5});
            // setTimeout(function(){
            //     location.href = '/login.html';
            // }, 1500);
            // return false;
        }
        
        // 表单提交
        form.on('submit(forumSubmit)', function(data){
            console.log('表单提交事件触发');
            var field = data.field;
            
            // 获取UEditor内容
            field.content = ue.getContent();
            // 表单验证
            if(!field.title){
                layer.msg('标题不能为空', {icon: 5});
                return false;
            }
            if(!field.type){
                layer.msg('请选择类型', {icon: 5});
                return false;
            }
            if(!field.content){
                layer.msg('内容不能为空', {icon: 5});
                return false;
            }
            var index = layer.load();

            // 防止重复提交
            if($(this).data('submitting')) {
                return false;
            }
            $(this).data('submitting', true);

            // 提交数据
            $.ajax({
                url: "{:url('Forum/forum_add')}",
                type: 'post',
                data: field,
                dataType: 'json',
                success: function (res) {
                    console.log('Ajax响应:', res);
                    layer.close(index);
                    if (res.code === 1) {
                        layer.msg(res.msg, {icon: 5});
                    } else {
                        layer.msg(res.msg, {icon: 1});
                        setTimeout(function () {
                            location.href = '{:url("Forum/forum")}';
                        }, 1500);
                    }
                },
                error: function(xhr, status, error) {
                    console.log('Ajax错误:', error);
                    layer.msg('网络错误，请稍后重试', {icon: 5});
                },
                complete: function() {
                    // 重置提交状态
                    $(this).data('submitting', false);
                }
            });
            
            return false;
        });
        
        // 重新渲染表单
        form.render();
    });
</script>

{include file="layout/footer"}
</body>
</html>